package com.xu.provider.serviceImpl;

import com.xu.dto.CardDto;
import com.xu.entity.Card;
import com.xu.provider.dao.CardDao;
import com.xu.service.CardService;
import com.xu.utils.Base64Util;
import com.xu.utils.PhoneVcardUtil;
import com.xu.vo.R;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author 徐老板
 * @date 2020/3/31   10:51
 */
//学到了 分布式dubbo中这样写业务层 service层要加component注解加到spring容器
// 还有@service注解加到zookeeper容器
@Component
@Service(version = "1.0.0")
public class CardServiceImpl implements CardService {

    private static String pre_url="http://localhost:9092//api/qrcode/cardqrcode.do?msg=";

    @Resource
    private CardDao cardDao;


    @Override
    public R checkPhone(String phone) {
        Card card = cardDao.selectByPhone(phone);
        if (card!=null){
            return R.fail();
        }else {
            return R.ok(null);
        }

    }

    @Override
    public R queryByPhone(String phone) {
        Card card = cardDao.selectByPhone(phone);
        if (card!=null){
            return R.fail();
        }else {
            return R.ok(card);
        }

    }

    @Override
    public R save(CardDto cardDto) {
        if (cardDto.getPhone()!=null&&cardDao.selectByPhone(cardDto.getPhone())==null){
                if (cardDao.insert(cardDto)>0){
                    //成功就返回对应的二维码 路径
                    String str = PhoneVcardUtil.createPV(cardDto);
                    //base64url的编码
                    String urlEnc = Base64Util.base64UrlEnc(str);
                    return R.ok(pre_url+urlEnc);
                }else {
                    return R.fail();
                }
        }else {
            return R.fail();
        }

    }

    @Override
    public R all() {
        return R.ok(cardDao.selectAll());
    }
}
